package com.wechat.back.utils.ACF;

import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ACFilter implements SensitiveWordFilter {
    private ACTrie acTrie = null;

    @Override
    public String filter(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        List<MatchResult> match = acTrie.match(text);
        if (match == null || match.isEmpty()) {
            return text;
        }

        // 构建包含敏感词位置和文本信息的字符串
        StringBuilder result = new StringBuilder(text);
        for (MatchResult matchResult : match) {
            result.append("\n敏感词: ").append(matchResult.getSensitiveWord())
                    .append(" 位于: ").append(matchResult.getStartIndex())
                    .append(" - ").append(matchResult.getEndIndex());
        }
        return result.toString();
    }

    @Override
    public boolean hasSensitiveWord(String text) {
        if (text == null || text.isEmpty()) {
            return false;
        }
        return!acTrie.match(text).isEmpty();
    }

    @Override
    public void loadSensitiveWord(List<String> sensitiveWordList) {
        if (sensitiveWordList == null || sensitiveWordList.isEmpty()) {
            return;
        }
        acTrie = new ACTrie(sensitiveWordList);
    }
}    